﻿Public Class frmPagIBIGDE
    Dim validator As PropertyValidator = New PropertyValidator
    'Dim sqlManager As New SQLManager
    'Dim sqlList As New SQLList
    Dim payrollManager As New PayrollManager
    Private Sub frmPagIBIGDE_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ClearAllFields()
        If Clipboard.GetData(DataFormats.Text) <> "" Then
            Try
                'Dim pagIBIGList As List(Of PagIBIG) = sqlList.getItemPagIBIG(Clipboard.GetData(DataFormats.Text))
                Dim pagIBIGList As List(Of PagIBIG) = payrollManager.getItemPagIBIG(Clipboard.GetData(DataFormats.Text))
                For Each pagIBIG As PagIBIG In pagIBIGList
                    txtMinimumSalary.Text = pagIBIG.min_salary
                    txtMaximumSalary.Text = pagIBIG.max_salary
                    txtEmployerShare.Text = pagIBIG.employer_share
                    txtEmployeeShare.Text = pagIBIG.employee_share
                    txtMaximumShare.Text = pagIBIG.min_share
                Next
                btnSave.Text = "&Update"
                btnClear.Text = "&Cancel"
            Catch ex As Exception
                MsgBox("Can't load Web page" & vbCrLf & ex.Message)
            End Try
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If validator.IsNumericString(txtMinimumSalary, "Minimum Salary") Then Return
        If validator.IsNumericString(txtMaximumSalary, "Maximum Salary") Then Return
        If validator.IsNumericString(txtEmployerShare, "Percentage Employee Share") Then Return
        If validator.IsNumericString(txtEmployeeShare, "Percentage Employer Share") Then Return
        If validator.IsNumericString(txtMaximumShare, "Maximum Share") Then Return

        Try
            If Clipboard.GetData(DataFormats.Text) = "" Then
                payrollManager.insertItemPagIBIG(txtMinimumSalary.Text, txtMaximumSalary.Text, txtEmployerShare.Text, txtEmployeeShare.Text, txtMaximumShare.Text)
                'sqlManager.executeQuery("INSERT INTO tbl_pagibig (min_salary, max_salary, employer_share, employee_share, min_share) VALUE (" + txtMinimumSalary.Text + "," + txtMaximumSalary.Text + "," + txtEmployerShare.Text + "," + txtEmployeeShare.Text + "," + txtMaximumShare.Text + ")")
                MsgBox("Successfully saved.")
            Else
                payrollManager.updateItemPagIBIG(txtMinimumSalary.Text, txtMaximumSalary.Text, txtEmployerShare.Text, txtEmployeeShare.Text, txtMaximumShare.Text, Clipboard.GetData(DataFormats.Text))
                'sqlManager.executeQuery("UPDATE tbl_pagibig SET min_salary=" + txtMinimumSalary.Text + ", max_salary=" + txtMaximumSalary.Text + ", employer_share=" + txtEmployerShare.Text + ", employee_share=" + txtEmployeeShare.Text + ", min_share=" + txtMaximumShare.Text + " WHERE id=" + Clipboard.GetData(DataFormats.Text) + "")
                MsgBox("Successfully updated.")
            End If
            ClearAllFields()
        Catch ex As Exception
            MsgBox("Can't load Web page" & vbCrLf & ex.Message)
        End Try
        Me.Close()
    End Sub

    Public Function ClearAllFields()
        validator.ResetControls(grpPagIBIG)
        btnSave.Text = "&Save"
        btnClear.Text = "&Clear"
        Return 0
    End Function

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        If Clipboard.GetData(DataFormats.Text) = "" Then
            validator.ResetControls(grpPagIBIG)
        Else
            Me.Close()
        End If
    End Sub
End Class